package org.dawfinn.droiditarian.view;

import org.dawfinn.droiditarian.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;

public abstract class StatusIcon extends RelativeLayout {

	private boolean checked;
	private View icon;
	private AnimationSet fadeOut;
	private AnimationSet fadeIn;
	private boolean useAnimations = true;

	public StatusIcon(Context context) {
		super(context);
		initLayout(context);
	}

	public StatusIcon(Context context, AttributeSet attrs) {
		super(context, attrs);
		initLayout(context);
	}

	public StatusIcon(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initLayout(context);
	}

	protected void initLayout(Context context) {
		LayoutInflater inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(getLayout(), this);

		this.icon = findViewById(getCheckedViewId());

		this.fadeOut = (AnimationSet) AnimationUtils.loadAnimation(getContext(),
				R.anim.fade_out);
		this.fadeIn = (AnimationSet) AnimationUtils.loadAnimation(getContext(),
				R.anim.fade_in);
	}

	protected abstract int getLayout();

	protected abstract int getCheckedViewId();

	public boolean isChecked() {
		return checked;
	}

	public void setChecked(boolean checked) {
		if (checked != this.checked) {
			if (checked) {
				icon.setVisibility(VISIBLE);
				if (useAnimations)
					icon.startAnimation(fadeIn);
			} else {
				if (useAnimations)
					icon.startAnimation(fadeOut);
				icon.setVisibility(INVISIBLE);
			}
		}
		this.checked = checked;
	}

	public void setUseAnimations(boolean useAnimations) {
		this.useAnimations = useAnimations;
	}

}